using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Db;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]

    //规格控制器
    public class SpecificationsController : ControllerBase
    {
        private IConfiguration _configuration;

        //规格
        private IRepository<Specifications> _specificationsRepository;

        //属性
        private IRepository<Model.Attribute> _attributeRepository;
        //属性值
        private IRepository<Attribute_value> _attributeValueRepository;

        public SpecificationsController(IConfiguration configuration, IRepository<Model.Attribute> attributeRepository, IRepository<Attribute_value> attributeValueRepository, IRepository<Specifications> specificationsRepository)
        {
            _configuration = configuration;
            _specificationsRepository = specificationsRepository;
            _attributeRepository = attributeRepository;
            _attributeValueRepository = attributeValueRepository;
        }


        //获取所有规格
        public dynamic Get()
        {
            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var shopingDb = new ShoppingCenterDb();

            // 分页
            var specifications = shopingDb.Specifications.Include(x => x.Attribute).Include(x => x.Attribute_value).ToList().Skip((currentPage - 1) * pageSize).Take(pageSize); ;



            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = specifications,
                    Pager = new { currentPage, pageSize, totalSize = specifications.Count() }
                },
                Msg = "获取规格表成功"
            };
        }

        //添加属性
        [HttpPost, Route("addAttribute")]
        public dynamic AddAttribute(CUR_Specifications newSpecifications)
        {
            var Attribute = _attributeRepository.Table.Where(x => x.Name == newSpecifications.Attribute);

            if (Attribute == null)
            {
                var att = new Model.Attribute
                {
                    Name = newSpecifications.Attribute
                };

                _attributeRepository.Insert(att);

                return new
                {
                    Code = 1000,
                    Data = att,
                    Msg = "添加新属性成功"
                };
            }


            return new
            {
                Code = 1000,
                Data = Attribute,
                Msg = "获取属性成功"
            };
        }

        //添加属性值
        [HttpPost, Route("addAttributeValue")]
        public dynamic AddAttributeValue(CUR_Specifications newSpecifications)
        {
            var attributeValue = _attributeValueRepository.Table.Where(x => x.Name == newSpecifications.Attribute_value);

            if (attributeValue == null)
            {
                var attValue = new Attribute_value
                {
                    Name = newSpecifications.Attribute_value
                };

                _attributeValueRepository.Insert(attValue);

                return new
                {
                    Code = 1000,
                    Data = attValue,
                    Msg = "添加新属性值成功"
                };
            }


            return new
            {
                Code = 1000,
                Data = attributeValue,
                Msg = "获取属性值成功"
            };
        }

        // 添加规格
        [HttpPost]
        public dynamic Post(CUR_Specifications newSpecifications)
        {
            //商品id不能为空，库存、价格（商品的每种规格都要重新定义价格）
            var merchandiseId = newSpecifications.MerchandiseId;

            if (merchandiseId <= 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "获取不到当前商品"
                };
            }

            var inventory = newSpecifications.Inventory;
            var price = newSpecifications.Price;

            if (inventory <= 0 || price <= 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前商品规格的库存或价格不能小于或等于0"
                };
            }


            // 判断获取到的属性和属性值存不存在
            var attribute = _attributeRepository.Table.Where(x => x.Name == newSpecifications.Attribute).FirstOrDefault();
            var attributeValue = _attributeValueRepository.Table.Where(x => x.Name == newSpecifications.Attribute_value).FirstOrDefault();

            if (attribute == null || attributeValue == null)
            {
                return new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "当前的属性或属性值不存在，请去分类参数添加商品属性或属性值"
                };
            }

            // 插入规格表
            var spec = new Specifications
            {
                AttributeId = attribute.Id,
                Attribute_valueId = attributeValue.Id,
                MerchandiseId = merchandiseId,
                Inventory = inventory,
                Price = price
            };

            _specificationsRepository.Insert(spec);

            return new
            {
                Code = 1000,
                Data = spec,
                Msg = "添加商品规格成功"
            };

            // 将结果实例化后输出

        }




    }
}